package com.fengpanlong.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fengpanlong.dao.DocumentDao;
import com.fengpanlong.dao.EmployeeDao;
import com.fengpanlong.entity.Document;
import com.fengpanlong.entity.Employee;
import com.fengpanlong.entity.EmployeeProject;
import com.fengpanlong.util.DocumentHelp;
import com.fengpanlong.util.StringUtil;

public class DocumentServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String method = request.getMethod();
		String m = request.getParameter("_m");
		if("GET".equalsIgnoreCase(method)) {
			if("shore".equals(m)){
				shoredocument(request,response);
			} else if("delete".equals(m)){
				deletedocumetn(request,response);
			} else if("save".equals(m)){
				savedocument(request,response);
			} else if("show".equals(m)){
				allshow(request,response);
			}
			
			else if("".equals(m) || m == null){
				showdocument(request,response);
			}
		} else{
			if("save".equals(m)){
			   shore(request,response);
			}
		}
	}
	

	private void allshow(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		DocumentDao documentDao = new DocumentDao();
		Document d = documentDao.findById(id);
		EmployeeDao edao = new EmployeeDao();
		Employee e = edao.findById(d.getEmployeeId());
		if(!"".equals(e) && e != null){
			request.setAttribute("e",e);
			request.setAttribute("time",StringUtil.formateTime(d.getCreatetime()));
			request.setAttribute("document", d);
			request.getRequestDispatcher("/WEB-INF/views/allDocument.jsp").forward(request, response);
		} else{
			System.out.print("不存在对象");
		}
	}


	private void savedocument(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		DocumentDao documentDao = new DocumentDao();
		Document d = documentDao.findById(id);
		request.setAttribute("document", d);
		request.getRequestDispatcher("/WEB-INF/views/edit_document.jsp").forward(request, response);
	}



	
	
	
	

	/**
	 * 删除文献
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void deletedocumetn(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		DocumentDao documentDao = new DocumentDao();
		documentDao.delete(id);
		response.sendRedirect("document.jspx");
	}


	/**
	 * 跳转到添加页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void shoredocument(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		request.getRequestDispatcher("/WEB-INF/views/new_document.jsp").forward(request, response);
		
	}
	
	/**
	 * 保存新资料
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void shore(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		String projectid = ep.getProjectId();
		String employeeid = ep.getEmployeeId();
		DocumentDao documentDao = new DocumentDao();
		String title = request.getParameter("document_title");
		String content = request.getParameter("document_content");
		String createtime = StringUtil.formateTime();
		String id = StringUtil.getId();
		Document document = new Document();
		document.setId(id);
		document.setContent(content);
		document.setTitle(title);
		document.setProjectId(projectid);
		document.setEmployeeId(employeeid);
		document.setCreatetime(createtime);
		documentDao.save(document);
		response.sendRedirect("document.jspx");
	}
	
	
	/**
	 * 跳转到显示页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showdocument(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException  {
	
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		String projectid = ep.getProject().getId();
		EmployeeDao edao = new EmployeeDao();
		DocumentDao documentDao = new DocumentDao();
		List<Document> list = documentDao.findAll(projectid);
		for(Document doc : list) { 
		Employee e = edao.findById(doc.getEmployeeId());
		doc.setEmployee(e);
		request.setAttribute("time",StringUtil.formateTime(doc.getCreatetime()));
		}
		
		request.setAttribute("documentlist",list);
		request.getRequestDispatcher("/WEB-INF/views/showDocument.jsp").forward(request, response);
	}
}
